<script setup>
import { useUserStore } from '@/stores/user';
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const userName = ref('');
const password = ref('');
const userStore = useUserStore();
const router = useRouter();
    // 读取用户信息

    // 设置用户信息
const checkUser = () => {
  const isAuthenticated = userStore.check(userName.value, password.value);
  if (isAuthenticated) {
    router.back();
  }
  else{
    if (userName.value === '' || password.value === ''){
      document.getElementById('error').innerHTML = '用户名或密码不能为空';
    }
    else {

    document.getElementById('error').innerHTML = '用户名或密码错误';
  }
  }
}
function register() {
  if (userName.value === '' || password.value === '') {
    document.getElementById('error').innerHTML = '用户名或密码不能为空';
    return;
  }
  if(userStore.register(userName.value, password.value)==="User already exists"){
    document.getElementById('error').innerHTML = '用户已被注册';
    return;
  }
  router.back();
}
</script>

<template>
  <div id="login-container">
    <div class="main">
      <div class="login-box">
        <div class="title">
          <h1>Register</h1>
        </div>
        <div id="login">
          <form action="" @submit.prevent="register()">
            <label for="name">Name:</label>
            <input type="text" id="name" v-model="userName" />
            <br />
            <label for="password">Password:</label>
            <input type="password" id="password" v-model="password"/>
            <br />
            <button type="button" @click="register()" id="login-button">Register</button>
            <div id="error"></div>
          </form>
        </div>
    </div>

  </div>
  <div class="message">
    <div class="title">
      <h2>现在注册即送Teio老师高等数学1对1辅导！</h2>
    </div>
    <div class="logo">
      <img src="@/assets/img/taidao.jpg" alt="Teio">
      </div>
  </div>
  </div>
</template>

<style scoped>
.login-box {
  margin-left: auto;
  margin-right: auto;
  padding: 20px;
  border-radius: 10px;
  position: relative;
  top: 10%;
}
.message {
  .title{
    text-align: center;
    margin-bottom: 50px;
    font-family: Arial, sans-serif;
    font-size: 24px;
    color: #333;
    font-weight: bold;
  }
  img {
    width: 700px;
    height: 600px;
    margin-bottom: 20px;
  }
}
#login-container {
  display: flex;
  background-color: lightblue;
}
.message {
  padding: 50px;
  border-radius: 10px;
  position: relative;
  top: 10%;
  width: 50%;
  text-align: center;
}
.main {
  width: 20%;
  height: 800px;
  padding: 50px;
  border-radius: 10px;
  background-color: #f2f2f2;
  margin-left: 100px;

}
.title {
  text-align: center;
  margin-bottom: 50px;
  font-family: Arial, sans-serif;
  font-size: 24px;
  color: #333;
}
#title h1 {
  font-size: 36px;
  margin-bottom: 20px;
}
#error {
  color: red;
}
#name, #password {
  margin-bottom: 10px;
  padding: 5px;
  border-radius: 5px;
  border: 1px solid #ccc;
}
#name {
  margin-right: 10px;
  width: 150px;
}
#login {
  margin-top: 50px;
  width: 300px;
  margin-left: auto;
  margin-right: auto;
}
#login-description {
  text-align: center;
  margin-bottom: 50px;
}
label {
  margin-bottom: 5px;
}
button {
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
input[type=button]:hover {
  background-color: #45a049;
}
input[type=text]:focus, input[type=password]:focus {
  outline: none;
  border: 1px solid #4CAF50;
}
#login-button:hover {
  background-color: #45a049;
  color: white;
}
#login-button:active {
  background-color: #3e8e41;
  color: white;
}
</style>